package utils.UI;

import java.awt.Color;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Line2D;

import javax.swing.JPanel;

public class PlotPanel extends JPanel{
	
	
		double[] data_x;
		double[] data_y;
		
		public PlotPanel(double[] x, double[] y) {
			if(x.length!=y.length)
				throw new IllegalArgumentException("invalid array size");
			
			this.data_x=x;
			this.data_y=y;
		}
		
	    final int PAD = 20;

	    protected void paintComponent(Graphics g) {
	        super.paintComponent(g);
	        Graphics2D g2 = (Graphics2D)g;
	        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
	                            RenderingHints.VALUE_ANTIALIAS_ON);
	        int w = getWidth();
	        int h = getHeight();
	        // Draw ordinate.
	        g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
	        // Draw abcissa.
	        g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
	        
	        double xInc = (double)(w - 2*PAD)/(data_x.length-1);
	        double scale = (double)(h - 2*PAD)/getMax();
	        // Mark data points.
	        g2.setPaint(Color.red);
	        
	        for(int i = 0; i < data_x.length; i++) {
	            double x = PAD + i*xInc;
	            double y = h - PAD - scale*data_y[i];
	            g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
	        }
	    }

	    private double getMax() {
	        double max = -Integer.MAX_VALUE;
	        for(int i = 0; i < data_x.length; i++) {
	            if(data_x[i] > max)
	                max = data_x[i];
	        }
	        return max;
	    }

}
